# -*- coding: utf-8 -*-
import sys

from django.core.urlresolvers import reverse

reload(sys)
sys.setdefaultencoding("utf-8")
import test_qiniu
from django.db import models
from django.contrib.auth.models import User
import time
from DjangoUeditor.models import UEditorField


class Category(models.Model):
    # 分类列表
    category_id = models.AutoField(primary_key=True)
    category_name = models.CharField(max_length=100, default='', help_text=u"分类名称")
    level = models.PositiveIntegerField(default=1, help_text=u"分类等级")
    sort_weight = models.PositiveIntegerField(default=0, help_text=u"排序")
    add_time = models.PositiveIntegerField(default=lambda: int(time.time()))
    edit_time = models.PositiveIntegerField(default=lambda: int(time.time()), help_text=u"编辑时间")
    # edit_user_id = models.PositiveIntegerField(default=0, help_text=u"编辑人id")
    edit_user_name = models.CharField(max_length=100, default='', help_text=u"编辑人名称")
    # parent_class_id = models.PositiveIntegerField(default=0, help_text=u"父级id")

    class Meta:
        verbose_name_plural = "分类"
        db_table = "category"
        app_label = 'zhp_blog'

class Article(models.Model):
    # 内容分类
    hid = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(auto_now_add=True) # 归档使用
    title = models.CharField(max_length=100, default='', help_text=u"标题")
    category_id = models.PositiveIntegerField(default=0, help_text=u"所在分类id")
    add_time = models.PositiveIntegerField(default=lambda: int(time.time()))
    edit_time = models.PositiveIntegerField(default=lambda: int(time.time()), help_text=u"发布时间")
    edit_user_name = models.CharField(max_length=100, default='', help_text=u"编辑人名称")
    is_like = models.PositiveIntegerField(default=0)  # 0 没有点赞  1 点赞
    like_count = models.PositiveIntegerField(default=1000)  # 点赞数量
    unlike_count = models.PositiveIntegerField(default=0)  # 踩灭数量
    # detail = UEditorField(u"正文", height=350, width=800, default=u'', blank=True,
    #  imagePath="uploads/zhp_blog/images/",toolbars='besttome', filePath='uploads/zhp_blog/files/')  # 详情

    detail = UEditorField(u"正文", height=500, width=1140, default=u'', blank=True,
                          upload_to="uploads/media/user_image",filePath="uploads/media/user_image"
                          )  # 详情

    count = models.PositiveIntegerField(default=1000)  # 次数

    def __unicode__(self):
        return self.title

    # 获取文章地址
    def get_absolute_url(self):
        return reverse('zhp_blog:detail',args=[self.hid])

    class Meta:
        ordering = ['-edit_time']
        verbose_name = "小苹果的分享"
        verbose_name_plural = "内容"
        db_table = "article"
        app_label = 'zhp_blog'


class Message(models.Model):
    """联系消息"""
    mid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    subject = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    content = models.TextField(blank=False)
    create_time = models.DateTimeField(auto_now_add=True)
    count = models.PositiveIntegerField(default=0)  # 次数

    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = "消息"
        db_table = "message"
        app_label = 'zhp_blog'

# 点赞表
class ArticleUpDown(models.Model):
    nid = models.AutoField(primary_key=True)
    article_id = models.PositiveIntegerField(default=0)
    is_up = models.BooleanField(default=True)  # 1 赞 0 灭
    user_ip = models.CharField(max_length=20, default='')

    def __str(self):
        return self.nid
    class Meta:
        db_table = "article_updown"

